package com.novabluetooth.services.servicedevices;

import java.util.UUID;

import com.novabluetooth.services.TJBluetoothLeService;
import com.novabluetooth.services.TJBluetoothLeService.LocalBinder;

import android.bluetooth.BluetoothGattService;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

/**
 * 三诺WL_1型血糖
 * 
 * @author nova
 *
 */
public class XueTangWL1SanNuoService extends TJBluetoothLeService {
	/**
	 * 三诺WL_1型血糖
	 */

	private String sino_readData = "534E0600040400000E";
	private String sino_closeBle = "534E0600040C000016";
	private String sino_closeData = "534E0600040B020017";
	/**
	 * sannuo
	 */
	public final static UUID serviceSanNuoUUID = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb");
	public final static UUID writeSanNuoCharactUUID = UUID.fromString("0000ffe2-0000-1000-8000-00805f9b34fb");
	public final static UUID readSanNuoCharactUUID = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");

	@Override
	public void servicesDiscovered() {
		super.servicesDiscovered();

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
			BluetoothGattService gattService = mBluetoothGatt.getService(serviceSanNuoUUID);
			writeGattCharact = gattService.getCharacteristic(writeSanNuoCharactUUID);
			readGattCharact = gattService.getCharacteristic(readSanNuoCharactUUID);
			mBluetoothGatt.setCharacteristicNotification(readGattCharact, true);
			ifSuccess = writeDataToDevice1(sino_readData, readGattCharact);
		}

	}

	@Override
	public boolean isRightData(byte[] buffer) {
		super.isRightData(buffer);
		int readSize = buffer.length;

		if (readSize <= 0) {

			ifSuccess = false;
		} else if (readSize < 15) {
			Log.e("LeeTag", "Data is Short");
			ifSuccess = false;
		} else if (((buffer[0] & 0xff) != 0x53)) {
			Log.e("LeeTag", "Error Data Packet Header");
			ifSuccess = false;
		} else {
			String resultStr = "" + (buffer[11] & 0xFF) + (buffer[12] & 0xff);
			float bg = (float) (Integer.parseInt(resultStr) / 10.0);
			// checkData = "血糖:" + bg;
			checkData = String.valueOf(bg);
			ifSuccess = true;
		}
		return ifSuccess;

	}

	@Override
	public void closeDevice() {
		super.closeDevice();
		ifSuccess = writeDataToDevice(sino_closeData);
		writeDataToDevice(sino_closeBle);
	}

}
